<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class files {

    private $config;
    private $ommit = array(".", "..");

    public function __construct($config) {
        $this->config = $config;
    }

    /**
     * Get extension from filename
     *
     * @param type $filename
     * @return string extension
     */
    public function extract_extension($filename) {
        $filename = strtolower($filename);
        $fnsplit = explode(".", $filename);
        return $fnsplit[count($fnsplit) - 1];
    }

    /**
     * Process file found in incoming folder
     *
     * @param type $file File name to process
     * @return boolean true if success, false if not
     */
    public function process_file($file) {
        if (empty($file))
            return false;
        $extension = $this->extract_extension($file);
        if (!in_array($extension, $this->config["Processing"]["allowed_formats"]))
            return false;
        if ($this->config["Processing"]["delete_sample"] && strstr(strtolower($file), "sample"))
            return false;
        $processed[] = $file;
        return $processed;
    }

    /**
     * Get content of folder (including other folders)
     *
     * @param type $getdir
     * @return int
     */
    public function get_folder($getdir) {
        $dirs = array();
        $dh = opendir($getdir);
        while ($dir = readdir($dh)) {
            if (in_array($dir, $this->ommit))
                continue;
            $fullpath = $getdir . "/" . $dir;
            $dirs[$dir]["name"] = $dir;
            if (is_file($fullpath))
                $dirs[$dir]["directory"] = 0;
            else
                $dirs[$dir]["directory"] = 1;
        }
        closedir($dh);
        return $dirs;
    }

    /**
     *
     * Delete directory and whole tree
     *
     * @param type $dir
     * @return type
     */
    public function delTree($dir) {
        if (DEBUG_SIMULATE)
            return true;
        if (is_file($dir)) {
            unlink($dir);
            return true;
        }
        $files = array_diff(scandir($dir), array('.', '..'));
        foreach ($files as $file) {
            $fullpath = $dir . "/" . $file;
            if (!chmod($fullpath, 0777))
                die("CHMOD problem, check permissions!");
            (is_dir($fullpath)) ? $this->delTree($fullpath) : unlink($fullpath);
        }
        if (strlen($dir) < strlen($this->config["Directories"]["source"]) + 3)
            return false;
        return rmdir($dir);
    }

    /**
     * Process folder found in incoming folder
     *
     * @param type $basedir
     * @param type $subfolder
     * @return boolean|string
     */
    public function process_subfolder($basedir, $subfolder) {
        if (empty($subfolder))
            return false;
        $processed = array();
        foreach ($subfolder as $item => $val) {
            if ($val["directory"]) {
                $val = $this->get_folder($basedir . "/" . $item);
                $val = array_pop($val);
                $val["name"] = $item . "/" . $val["name"];
            }
            $extension = $this->extract_extension($val["name"]);
            if (in_array($extension, $this->config["Processing"]["allowed_formats"]))
                if ($this->config["Processing"]["delete_sample"] && strstr(strtolower($val["name"]), "sample"))
                    continue;
                else
                    $processed[] = $basedir . $val["name"];
        }
        return $processed;
    }

}

?>
